# Copyright 2022 Flant JSC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

{!{/*
Multifile generator of e2e workflows.

One workflow file for each provider.

Workflow consists of jobs for each possible pair of CRI and Kubernetes version.
Jobs are enabled according to outputs from check labels job.

A note on werf.yaml and deckhouse image tag:

$CI_COMMIT_REF_NAME environment variable is used in werf.yaml, so it is needed
    for successful run of werf commands.

$CI_COMMIT_REF_SLUG is a tag of published deckhouse images. It has a form
    of "pr<PR_NUMBER>" for pull requests and deckouse Deployment uses this tag
    to auto update testing cluster on new commits.
    The Git tag is slugified in case it constains a plus sign.

*/}!}

{!{- $providerNames := slice "AWS" "Azure" "GCP" "Yandex.Cloud" "OpenStack" "vSphere" "Static" "EKS" -}!}
{!{- $criNames := slice "Containerd" -}!}
{!{- $kubernetesVersions := slice "1.25" "1.26" "1.27" "1.28" "1.29" "Automatic" -}!}

{!{- range $providerName := $providerNames -}!}
{!{-   $provider := $providerName | replaceAll "." "-" | toLower -}!}
{!{-   $kubernetesDefaultVersion := (tmpl.Exec "e2e_kubernetes_default_version" $ | strings.TrimSpace ) -}!}
{!{-   $ctx := dict "provider" $provider "providerName" $providerName "criNames" $criNames "kubernetesVersions" $kubernetesVersions "kubernetesDefaultVersion" $kubernetesDefaultVersion }!}
{!{-   $outFile := printf "e2e-abort-%s.yml" $provider }!}
{!{-   $outPath := filepath.Join (getenv "OUTDIR") $outFile }!}
{!{-   tmpl.Exec "e2e_clean_workflow_template" $ctx | file.Write $outPath }!}
{!{- end -}!}


{!{/* Template with e2e jobs for one provider. */}!}
{!{- define "e2e_clean_workflow_template" -}!}
{!{- $ctx := . -}!}
{!{- $workflowName := printf "destroy cluster: %s" $ctx.providerName -}!}
# <template: e2e_workflow_template>
name: '{!{ $workflowName }!}'
on:
  workflow_dispatch:
    inputs:
      run_id:
        description: 'GitHub Action running id with failed e2e test'
        required: true
      state_artifact_name:
        description: 'GitHub artifact name with dhctl/terraform status'
        required: true
      cluster_prefix:
        description: 'Dhctl cluster prefix'
        required: true
      issue_number:
        description: 'ID of comment in issue with creation message'
        required: true
      comment_id:
        description: 'ID of comment in issue where to put workflow run status'
        required: true
      layout:
        description: 'Cloud provider layout which was tested'
        required: true
      cri:
        description: 'CRI which was tested'
        required: true
      k8s_version:
        description: 'CRI which was tested'
        required: true
      # needs for run correct installer image for abort
      installer_image_path:
        description: 'Installer image without host'
        required: true
      # needs for destroy cloud clusters if cluster was bootstrapped fully, but e2e was not pass
      ssh_master_connection_string:
        description: 'SSH connection string'
        required: false
env:
{!{ tmpl.Exec "werf_envs" | strings.Indent 2 }!}

# Note: no concurrency section for e2e workflows.
# Usually you run e2e and wait until it ends.

jobs:
  started_at:
    name: Save start timestamp
    outputs:
      started_at: ${{ steps.started_at.outputs.started_at }}
    runs-on: "ubuntu-latest"
    steps:
{!{ tmpl.Exec "started_at_output" . | strings.Indent 6 }!}

{!{/* Jobs for each CRI and Kubernetes version */}!}
{!{/* Note: it is sufficient to have one workflow 'e2e-abort.yaml' with one 'destroy' job and use provider, cri, k8s_version and layout from inputs. Still there are jobs for each CRI and Kubernetes version for similarity with e2e.multi.yaml */}!}
{!{- $lastCommentNeeds := slice "started_at" -}!}
{!{- $jobNames := dict -}!}
{!{- range $criName := $ctx.criNames -}!}
{!{-   range $kubernetesVersion := $ctx.kubernetesVersions -}!}
{!{-     $kubernetesVersionSlug := $kubernetesVersion | replaceAll "." "_" | toLower -}!}
{!{-     $cri := $criName | toLower -}!}
{!{-     $criEnv := $cri | toUpper -}!}
{!{-     $layout := (tmpl.Exec "e2e_get_layout" $ctx | strings.TrimSpace ) -}!}
{!{-     $jobID := printf "run_%s_%s" $cri $kubernetesVersionSlug -}!}
{!{-     $jobName := printf "%s, %s, Kubernetes %s" $workflowName $criName $kubernetesVersion -}!}
{!{-     $lastCommentNeeds = $lastCommentNeeds | append $jobID -}!}
{!{-     $jobNames = coll.Merge $jobNames (dict $jobID $jobName) }!}
{!{-     $jobCtx := coll.Merge $ctx (dict "manualRun" "yes" "cri" $cri "criName" $criName "criEnv" $criEnv "layout" $layout "kubernetesVersion" $kubernetesVersion "kubernetesVersionSlug" $kubernetesVersionSlug "workflowName" $workflowName "jobName" $jobName "jobID" $jobID) }!}
{!{     tmpl.Exec "e2e_clean_job_template" $jobCtx | strings.Indent 2 }!}
{!{-   end -}!}
{!{- end }!}

  last_comment:
    name: Update comment on finish
    needs: {!{ $lastCommentNeeds | toJSON }!}
    if: ${{ always() }}
    runs-on: ubuntu-latest
    env:
      JOB_NAMES: |
        {!{ $jobNames | toJSON }!}
    steps:
{!{ tmpl.Exec "checkout_step" $ctx | strings.Indent 6 }!}
{!{ tmpl.Exec "update_comment_on_finish" (slice "workflow,final,no-skipped,restore-separate" $workflowName) | strings.Indent 6 }!}

# </template: e2e_workflow_template>
{!{ end -}!}
